package com.code.petapp.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.code.petapp.R;
import com.code.petapp.bean.OrderBean;

import java.util.List;

public class OrderImgAdapter extends RecyclerView.Adapter<OrderImgAdapter.MyViewHolder> {
    private Context context;
    private List<OrderBean> list;
    private View inflater;
    private OnItemClickListener mListener;


    public OrderImgAdapter(Context context, List<OrderBean> list) {
        this.context = context;
        this.list = list;
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        inflater = LayoutInflater.from(context).inflate(R.layout.order_img_item, parent, false);
        MyViewHolder myViewHolder = new MyViewHolder(inflater);
        return myViewHolder;
    }
    public interface OnItemClickListener {
        void onClick(View parent, int position);
        void onClickMore(View parent, int position);
    }
    // 设置点击事件
    public void setOnItemClickListener(OnItemClickListener l) {
        this.mListener = l;
    }

    @SuppressLint("SetTextI18n")
    @Override
    public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
        OrderBean orderBean = list.get(position);
        holder.name.setText(orderBean.getGoodsName());
        holder.num.setText(orderBean.getNum().toString());
        holder.time.setText(orderBean.getTime());
        holder.orderNum.setText(orderBean.getOrderId());
        holder.price.setText(orderBean.getPrice().toString());
        holder.status.setText(orderBean.getStatus());
        Glide.with(context)
                .load(orderBean.getGoodsImg())
                .into(holder.img);
        if(orderBean.getStatus().equals("已完成")){
            holder.sureBtn.setEnabled(false);
        }
        if(null != mListener) {
            holder.sureBtn.setOnClickListener(v -> mListener.onClick(holder.sureBtn, position));
            holder.textMore.setOnClickListener(v -> mListener.onClickMore(holder.textMore, position));
        }
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    class MyViewHolder extends RecyclerView.ViewHolder {
        TextView name;
        ImageView img;
        TextView num;
        TextView time;
        TextView orderNum;
        TextView textMore;
        TextView price;
        TextView status;
        Button sureBtn;;


        public MyViewHolder(View itemView) {//这个view参数就是recyclerview子项的最外层布局
            super(itemView);
            //可以通过findViewById方法获取布局中的TextView
            name =itemView.findViewById(R.id.name);
            img =itemView.findViewById(R.id.img);
            num =itemView.findViewById(R.id.num);
            time =itemView.findViewById(R.id.time);
            orderNum =itemView.findViewById(R.id.orderNum);
            textMore =itemView.findViewById(R.id.textMore);
            price =itemView.findViewById(R.id.price);
            status =itemView.findViewById(R.id.status);
            sureBtn =itemView.findViewById(R.id.sureBtn);
        }
    }
}
